
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
  },

  /**
   * 组件的初始数据
   */
  data: {
    isph: true,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showClose() {
      this.setData({
        show: false
      });
    },
    loginClose() {
      this.hideLogin()
    },
    //查看用户是否有手机号
    getUserPhone: function () {
      var that = this;
      that.setData({
        show: true
      });
    },
    //需要用户点击，获取微信用户手机号
    getUserPhoneByWeChat: function (e) {
      console.log(e);
      var that = this;
      if (that.data.isph) {
        that.setData({
          isph: false
        })
        var detail = e.detail;
        // var uid = wx.getStorageSync("uid");
        var uid = wx.getStorageSync("uid")
        var data = {
          encryptedData: detail.encryptedData,
          ivdata: detail.iv,
          uid: uid
        }
        // var code = wx.getStorageSync("loginCode");
        wx.login({
          fail: function (res) {
            wx.showToast({
              title: '登录失败',
              icon: 'none',
              duration: 2000
            })
            that.setData({
              isph: true
            })
          },
          success: function (res) {
            data.code = res.code
            if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
              that.setData({
                modalstatus: true,
                isph: true
              })
            } else {
              // data.code = code;
              that.decryptPhone(data);
              console.log('授权', data)
            }
            // wx.setStorageSync('loginCode', res.code);
            // data.code = res.code;
            // that.decryptPhone(data)
          }
        });
        // wx.checkSession({
        //   success() {
        //     if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
        //       that.setData({
        //         modalstatus: true
        //       })
        //     } else {
        //       wx.login({
        //         success: function (res) {
        // wx.setStorageSync('loginCode', res.code);
        //         data.code = res.code;
        //         that.decryptPhone(data);
        //       }
        //     });
        //     that.decryptPhone(data);
        //   }
        // },
        // fail() {
        //   wx.login({
        //     success: function (res) {
        // wx.setStorageSync('loginCode', res.code);
        //         data.code = res.code;
        //         that.decryptPhone(data);
        //       }
        //     });
        //   }
        // })
      }
    },
    //解密手机号数据
    decryptPhone: function (data) {
      var that = this
      var uid = wx.getStorageSync("uid")
      app.req({
        url: 'blessmini/user/deciphering?uid=' + uid,
        data: data,
        method: 'POST',
        success: function (res) {
          if (res.code === 200) {
            if (res.data.uid) {
              wx.setStorageSync('uid', res.data.uid);
            }
            wx.setStorageSync('phone', res.data.telPhone);
            that.setData({
              show: false,
              isph: true
            });
            that.hideLogin()
          } else {
            that.setData({
              isph: true
            })
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      });
    },
    // 传递给父组件
    hideLogin: function (e) {
      var that = this;
      var myEventDetail = {
        phoneshow: false,
        type: '取消'
      } // detail对象，提供给事件监听函数
      this.triggerEvent('myevent', myEventDetail) //myevent自定义名称事件，父组件中使用
    }
  }
})